#include "TROOT.h"
#include "TFile.h"
#include "TChain.h"
#include "TClonesArray.h"

#include "AnalysisTools/TCheatingAnalysisUtils.hxx"

#include "ND280Utils.h"
/*

ln -s $NDANADIR 
.x load.C 
.L ana0.C+

*/
 
void ana0(char * inputfilename, int max_entries = -1)
{
  TChain ch = TChain("TruthDir/Vertices");
  std::cout << "Adding file(s) matching string: " << inputfilename << std::endl;
  std::cout << "Added "<< ch.AddFile(inputfilename) << " file(s)"<< std::endl;
  Int_t NVtx;
  TClonesArray *Vertices = new TClonesArray("ND::TTruthVerticesModule::TTruthVertex", 100);
  
  ch.SetBranchAddress("NVtx", &NVtx);
  ch.SetBranchAddress("Vertices", &Vertices);

  TCheatingAnalysisUtils cheatUtils;

  int entries = ch.GetEntries();
  if(max_entries > 0 && max_entries<entries) entries = max_entries;
  std::cout << "Looping over " << entries<< " events " << std::endl;
  for(int ient = 0; ient < entries; ient++)
    {
      ch.GetEntry(ient);
      cout << "\n======== Getting event/entry "<< ient <<" ==========" << endl;
      cout << "     -> the TClonesArray contains "<< NVtx
           << " vertices" << endl;
      // Now loop over the lepton vertices we have found.                                                                                              
      ND::TTruthVerticesModule::TTruthVertex * curr_vtx;
      for(int i=0;i<NVtx;i++){
        // Example of accessing a data member of a TruthVertex.                                                                                        
        curr_vtx = (ND::TTruthVerticesModule::TTruthVertex *) Vertices->At(i);
        cout << "              ---> trajectory "<< i << " is for particle with pdg = "<< curr_vtx->NeutrinoPDG << endl;

        if( curr_vtx->NeutrinoPDG == ND280Utils::kPDGNumu){
          printf("one neutrino!!\n");
        }

      }
      
    } // eventloop                                                                                                                                           
  
  return;
}
